import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User } from "../../entities/user.entities";
import { TypeOrmCrudService } from '@nestjsx/crud-typeorm';

@Injectable()
export class UserService extends TypeOrmCrudService<User>{
  constructor(
    @InjectRepository(User)
    public readonly userRepository:Repository<User>
  ) {
    super(userRepository);
  }

  async findOneByName(username: string): Promise<User | undefined> {
    return await this.userRepository.findOne({
      where: {
        username: username
      }
    });
  }

  async findOneByID(id: number): Promise<User | undefined> {
    return await this.userRepository.findOne({
      where: {
        id: id
      }
    });

  }

  async saveUser(user:User){
    return await this.userRepository.save(user);
  }
}
